//分页函数 第一页: url/1 第二页: url/2 ... function pages($url, $totalnum, $page, $pagesize = 20) { $urladd = ''; $url .= '/'; $totalpage = ceil($totalnum / $pagesize); if($totalpage < 2) return ''; $page = min($totalpage, $page); $shownum = 5; // 显示多少个页 * 2 $start = max(1, $page - $shownum); $end = min($totalpage, $page + $shownum); // 不足 $shownum,补全左右两侧 $right = $page + $shownum - $totalpage; $right > 0 && $start = max(1, $start -= $right); $left = $page - $shownum; $left < 0 && $end = min($totalpage, $end -= $left); $s = ''; $page != 1 && $s .= '◀'; if($start > 1) $s .= '1 '.($start > 2 ? '... ' : '').''; for($i=$start; $i<=$end; $i++) { if($i == $page) { $s .= ''.$i.'';// checked } else { $s .= ''.$i.''; } } if($end != $totalpage) $s .= ''.($totalpage - $end > 1 ? '... ' : '').$totalpage.''; $page != $totalpage && $s .= '▶'; return $s;} function mid($n, $min, $max) { if($n < $min) return $min; if($n > $max) return $max; return $n;}function page($page, $n, $pagesize) { $total = ceil($n / $pagesize); $total < 1 AND $total = 1; return mid($page, 1, $total);}
后端部分,我们几句就可以搞定。
$pagesize = 20; //每页显示多少用户$n = user_count(); //总用户数$page = page($page, $n, $pagesize); //当前页数$userlist = user_find($page, $pagesize); //查找出当前页的用户//html部分,分页效果,直接渲染到页面即可$pagehtml = pages(url前缀, $n, $page, $pagesize);